-----------------------------------------------------------------------------
-- 1.lsp
-- 2.补全
-----------------------------------------------------------------------------

return {

	---------------------------------------
	-- lsp config
	-- https://github.com/neovim/nvim-lspconfig
	-- https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
	-- Configurations for Nvim LSP
	---------------------------------------
	{
		"neovim/nvim-lspconfig",
		lazy = false,
		config = function()
			--------------------------------------
			-- 自定义diagnostic图标
			vim.diagnostic.config({
				virtual_text = true,
				signs = true,
				update_in_insert = false,
			})

			local signs = { Error = " ", Warn = " ", Hint = " ", Info = " " }
			for type, icon in pairs(signs) do
				local hl = "DiagnosticSign" .. type
				vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = hl })
			end
			--------------------------------------

			local lspconfig = require("lspconfig")
			-- 安装列表
			-- { key: 服务器名， value: 配置文件 }
			-- key 必须为下列网址列出的 server name，不可以随便写
			local servers = {
				lua_ls = require("lspconfigs.lua"),
				-- sumneko_lua = require("lspconfigs.lua"),
				bashls = require("lspconfigs.bash"),
				jsonls = require("lspconfigs.json"),
				yamlls = require("lspconfigs.yamlls"),
				golangci_lint_ls = require("lspconfigs.golangci_lint_ls"),
				gopls = require("lspconfigs.gopls"),
				-- phpactor = require("lspconfigs.php"),
				pyright = require("lspconfigs.pyright"),
				html = require("lspconfigs.html"),
				cssls = require("lspconfigs.css"),
				emmet_ls = require("lspconfigs.emmet"),
				rust_analyzer = require("lspconfigs.rust"),
				cssmodules_ls = require("lspconfigs.cssmodules"),
				volar = require("lspconfigs.vue"),
				eslint = require("lspconfigs.eslint"),
				remark_ls = require("lspconfigs.markdown"),
				sqlls = require("lspconfigs.sqlls"),
				clangd = require("lspconfigs.clangd"),
				erlangls = require("lspconfigs.erlangls"),
				--- https://github.com/lighttiger2505/sqls
			}

			local default_opts = {
				capabilities = require("cmp_nvim_lsp").default_capabilities(),
				flags = {
					debounce_text_changes = 150,
				},
				on_attach = function(client, bufnr)
					-- 禁用格式化功能，交给专门插件插件处理
					client.server_capabilities.document_formatting = false
					client.server_capabilities.document_range_formatting = false
					-- local function buf_set_keymap(...)
					--     vim.api.nvim_buf_set_keymap(bufnr, ...)
					-- end
					-- require("keybinding").mapLSP(buf_set_keymap)
				end,
			}

			for name, config in pairs(servers) do
				if config ~= nil and type(config) == "table" and config.on_setup then
					-- 自定义初始化配置文件必须实现on_setup 方法
					-- print('init lsp', name)
					config.on_setup(lspconfig[name], default_opts)
				else
					-- 使用默认参数
					lspconfig[name].setup(default_opts)
				end
			end
		end,
	},

	-- https://github.com/williamboman/mason.nvim
	{
		"williamboman/mason.nvim",
		lazy = false,
		priority = 52,
		config = function()
			require("mason").setup()
		end,
	},

	---------------------------------------
	-- 自动安装lsp server
	-- https://github.com/williamboman/mason-lspconfig.nvim
	---------------------------------------
	{
		"williamboman/mason-lspconfig.nvim",
		lazy = false,
		priority = 51,
		opts = {
			ensure_installed = {
				"lua_ls",
			},
			automatic_installation = true,
		},
	},

	-- https://github.com/glepnir/lspsaga.nvim
	-- "glepnir/lspsaga.nvim",
	{
		"nvimdev/lspsaga.nvim",
		dependencies = {
			"nvim-treesitter/nvim-treesitter", -- optional
			"nvim-tree/nvim-web-devicons", -- optional
		},
		config = function()
			local lspsaga = require("lspsaga")
			lspsaga.setup({ -- defaults ...
				debug = false,
				use_saga_diagnostic_sign = true,
				-- diagnostic sign
				error_sign = "",
				warn_sign = "",
				hint_sign = "",
				infor_sign = "",
				diagnostic_header_icon = "   ",
				-- code action title icon
				code_action_icon = " ",
				code_action_prompt = {
					enable = true,
					sign = true,
					sign_priority = 40,
					virtual_text = true,
				},
				finder_definition_icon = "  ",
				finder_reference_icon = "  ",
				max_preview_lines = 10,
				finder_action_keys = {
					-- open = "o",
					open = "<CR>",
					vsplit = "s",
					split = "i",
					-- quit = "q",
					quit = "<ESC>",
					scroll_down = "<C-f>",
					scroll_up = "<C-b>",
				},
				code_action_keys = {
					-- quit = "q",
					quit = "<ESC>",
					exec = "<CR>",
				},
				rename_action_keys = {
					-- quit = "<C-c>",
					quit = "<ESC>",
					exec = "<CR>",
				},
				definition_preview_icon = "  ",
				border_style = "single",
				rename_prompt_prefix = "➤",
				rename_output_qflist = {
					enable = false,
					auto_open_qflist = false,
				},
				server_filetype_map = {},
				diagnostic_prefix_format = "%d. ",
				diagnostic_message_format = "%m %c",
				highlight_prefix = false,
			})
		end,
	},

	-- https://github.com/neovim/nvim-lspconfihrsh7th/vim-vsnipg
	-- https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
	-- lsp 加载进度ui
	{
		"j-hui/fidget.nvim",
		tag = "legacy",
		config = function()
			local fidget = require("fidget")
			fidget.setup({})
		end,
	},

	-- ## 用nvim做lauguage server
	-- https://github.com/jose-elias-alvarez/null-ls.nvim
	{
		"jose-elias-alvarez/null-ls.nvim",
		enabled = false,
		dependencies = "nvim-lua/plenary.nvim",
		config = function()
			require("lsp.null-ls")
		end,
	},

	---------------------------------------
	-- formatter
	-- https://github.com/mhartington/formatter.nvim
	-- github仓库上有预定义好的file type formatter
	---------------------------------------
	{
		"mhartington/formatter.nvim",
		config = function()
			local formatter = require("formatter")
			formatter.setup({
				-- Enable or disable logging
				logging = true,
				-- Set the log level
				log_level = vim.log.levels.DEBUG,
				filetype = {
					lua = {
						require("formatter.filetypes.lua").stylua,
					},
					go = {
						require("formatter.filetypes.go").goimports,
						require("formatter.filetypes.go").gofmt,
						require("formatter.filetypes.go").golines,
					},
					c = {
						require("formatter.filetypes.c").clangformat,
					},
				},
			})
			-- format on save
			vim.api.nvim_exec(
				[[
    augroup FormatAutogroup
    autocmd!
    autocmd BufWritePost *.逗号分隔的文件格式 FormatWrite
    augroup END
    ]],
				true
			)
		end,
	},
}
